var e = getApp(), t = require("../../../utils/md5.js");

Page({
    data: {
        rightBtnTxt: "",
        card: {
            pic: "",
            name: "",
            dept: "",
            mobile: "",
            wx_name: "",
            phone: "",
            email: "",
            remark: "",
            address: ""
        },
        feild: "",
        value: "",
        inputValue: "",
        editVisible: !1,
        server: e.server
    },
    cardTap: function(e) {
        switch (e.detail.btn) {
          case "left":
            break;

          case "center":
            this.save();
            break;

          case "right":
            this.changeStatus();
        }
    },
    apply: function() {
        e.request({
            url: e.server + "/userApi/applyAgent",
            method: "POST",
            data: {
                token: e.token,
                userToken: this.data.usr.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                0 === e.data.code ? wx.showToast({
                    icon: "none",
                    title: "申请提交成功"
                }) : wx.showToast({
                    icon: "none",
                    title: e.data.msg.zh
                });
            }
        });
    },
    changeStatus: function() {
        var t = this;
        1 === this.data.agent_status ? e.request({
            url: e.server + "/userApi/agentCancelAgent",
            method: "POST",
            data: {
                userToken: this.data.usr.userToken,
                token: e.token
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                0 === e.data.code && t.loadCard(), wx.showToast({
                    icon: "none",
                    title: e.data.msg.zh
                });
            }
        }) : this.apply();
    },
    showChoose: function() {
        var e = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var s = a.tempFilePaths, n = Date.now(), i = t("fitment_" + n);
                if (s && s.length > 0) {
                    var o = Date.now();
                    wx.uploadFile({
                        url: e.data.server + "/uploadFile",
                        filePath: s[0],
                        name: "Filedata",
                        formData: {
                            userToken: e.data.usr.userToken,
                            timestamp: o,
                            token: i,
                            type: "images"
                        },
                        success: function(t) {
                            var a = JSON.parse(t.data);
                            0 === a.code ? e.setData({
                                "card.pic": a.result.file
                            }) : wx.showToast({
                                icon: "none",
                                title: a.msg.zh
                            });
                        }
                    });
                }
            }
        });
    },
    save: function() {
        var t = {
            userToken: this.data.usr.userToken
        };
        for (var a in this.data.card) t[a] = this.data.card[a];
        e.request({
            url: e.server + "/userApi/updateVisitingCard",
            method: "POST",
            data: t,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                wx.showToast({
                    icon: "none",
                    title: e.data.msg.zh
                });
            }
        });
    },
    onLoad: function(e) {},
    loadCard: function() {
        var t = this;
        this.loadUsr(), e.request({
            url: e.server + "/userApi/getMyVisitingCard",
            method: "POST",
            data: {
                token: e.token,
                userToken: this.data.usr.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                if (0 === e.data.code) {
                    var a = "";
                    a = 1 === e.data.result.info.agent_status ? "取消加入" : "申请加入", t.setData({
                        card: e.data.result.info,
                        agent_status: e.data.result.info.agent_status,
                        rightBtnTxt: a
                    });
                } else t.setData({
                    card: {
                        pic: "",
                        name: "",
                        dept: "",
                        mobile: "",
                        wx_name: "",
                        phone: "",
                        email: "",
                        remark: "",
                        address: ""
                    },
                    rightBtnTxt: ""
                });
            }
        });
    },
    loadUsr: function() {
        var e = wx.getStorageSync("usr");
        this.setData({
            usr: e
        });
    },
    feildtap: function(e) {
        this.setData({
            editVisible: !0,
            feild: e.detail.feild,
            value: this.data.card[e.detail.feild] || "",
            placeholder: e.detail.tip
        });
    },
    feildOk: function() {
        this.setData({
            editVisible: !1
        });
        var e = {};
        e["card." + this.data.feild] = this.data.inputValue, console.log(e), this.setData(e), 
        this.data.inputValue;
    },
    feildInput: function(e) {
        this.setData({
            inputValue: e.detail.value
        });
    },
    closeEdit: function() {
        this.setData({
            editVisible: !1
        });
    },
    onReady: function() {},
    onShow: function() {
        this.loadCard();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});